<?php
/**
 * AdController
 *
 * @author  dani remeseiro - EcoEureka Segundamanita
 * @abstract this is the Ad controller ,
 * do the crud relative to ads : create, show, edit, delete
 */
class AdController extends Zend_Controller_Action {

    public function init() {

        $this->lang = $this->view->lang = $this->_helper->checklang->check();
        $this->location = $this->_helper->checklocation->check();
        $this->view->checkMessages = $this->_helper->checkMessages->check();
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->view->mensajes = $this->_flashMessenger->getMessages();


        //check if user is locked
        $locked = $this->_helper->checkLockedUser->check();
        if ($locked == 1) {
            $this->_redirect('/' . $this->view->lang . '/auth/logout');
        }

        if ($this->view->checkMessages > 0) {
            $this->_helper->_flashMessenger->addMessage($this->view->translate('You have') . ' ' .
                    '<a href="/' . $this->view->lang . '/message/received">' . $this->view->translate('new messages') . ' (' . $this->view->checkMessages . ')</a>');
        }
    }

    public function listAction() {

        $this->view->userRole = $this->_helper->checkUserRole->check();
       
        $woeid = $this->_request->getParam('woeid');
        $this->view->ad_type = $ad_type = $this->_request->getParam('ad_type');

        if ($ad_type == 'give') {
            $this->view->page_title .= $this->view->translate('Give') . ' - ';
            $type = 'give';
        }
        elseif ($ad_type == 'want') {
            $this->view->page_title .= $this->view->translate('Want') . ' - ';
            $type = 'want';
        }
        else {
            //dont accept other values than give/want
            $this->_helper->_flashMessenger->addMessage($this->view->translate('this url does not exist'));
            $this->_redirect('/' . $this->lang . '/woeid/' . $this->location . '/give');
        }
        
        $this->view->status = $status = $this->_request->getParam('status');
        $f = new Zend_Filter();
        $f->addFilter(new Zend_Filter_HtmlEntities());
        $status = $f->filter($status);

        if ($status) {
            $this->view->page_title .= $this->view->translate($status) . ' - ';
        }


        $model = new Model_Ad();
        $this->view->woeid = $woeid;
        $this->view->ad = $model->getAdList($woeid, $ad_type, $status);
        $this->view->woeidName = $this->_helper->woeid->name($woeid, $this->lang);
        $short = explode(',', $this->view->woeidName);
        $this->view->woeidNameShort = ' ' . $this->view->translate('in') . ' ' . $short[0];


        //add the link to the proper rss to layout
        $this->view->headLink()->appendAlternate('http://' . $_SERVER['HTTP_HOST'] . '/' . $this->lang . '/rss/feed/woeid/' . $woeid . '/ad_type/' . $ad_type . '/status/' . $status,
                'application/rss+xml',
                $this->view->woeidName . ' - ' . $this->view->translate((string) $type));



        if (empty($this->view->ad)) {
            $this->view->suggestIP = $this->_helper->getLocationGeoIP->suggest();
        }


        //TODO , this sucks, do a better way to not show invalid woeids or null
        if ((empty($woeid) ) || ($woeid < 10) || ($woeid == 29370606)) { //29370606 españa town
            $this->_helper->_flashMessenger->addMessage($this->view->translate('This location is not a valid town. Please, try again.'));
            $this->_redirect('/' . $this->lang . '/location/change');
        }

         //set the location name reg var from the woeid helper
        $aNamespace = new Zend_Session_Namespace('Segundamanita');
        $aNamespace->locationName = $this->view->woeidName;
        $this->view->page_title .= $this->view->woeidName;

        //paginator
        $page = $this->_getParam('page');

        if ($page) {
            $this->view->page_title .= ' - ' . $this->view->translate('page') . ' ' . $page;
        }

        $paginator = Zend_Paginator::factory($this->view->ad);
        $paginator->setDefaultScrollingStyle('Elastic');
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber($page);

        $this->view->paginator = $paginator;
    }

    public function listallAction() {

        $this->view->userRole = $this->_helper->checkUserRole->check();
        //var_dump($this->view->userRole);

        $model = new Model_Ad();

        $ad_type = $this->view->ad_type = $this->_request->getParam('ad_type');

        if ($ad_type == 'give') {
            $this->view->page_title .= $this->view->translate('Give') . ' - ';
            $type = 'give';
        }
        elseif ($ad_type == 'want') {
            $this->view->page_title .= $this->view->translate('Want') . ' - ';
            $type = 'want';
        }
        else {
            //dont accept other values than give/want
            $this->_helper->_flashMessenger->addMessage($this->view->translate('this url does not exist'));
            $this->_redirect('/' . $this->lang . '/ad/listall/ad_type/give');
        }

        $status = $this->_request->getParam('status');
        $f = new Zend_Filter();
        $f->addFilter(new Zend_Filter_HtmlEntities());
        $status = $f->filter($status);

        if ($status) {
            $this->view->page_title .= $this->view->translate($status) . ' - ';
        }

        $this->view->ad = $model->getAdListAll($ad_type, $status);
        $this->view->page_title .= $this->view->translate('All the ads');

        //paginator
        $page = $this->_getParam('page');
        if ($page) {
            $this->view->page_title .= ' - ' . $this->view->translate('page') . ' ' . $page;
        }
        
        $paginator = Zend_Paginator::factory($this->view->ad);
        $paginator->setDefaultScrollingStyle('Elastic');
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber($page);

        $this->view->paginator = $paginator;
    }
    
    /*********************************************/
    public function orgsAction() {
        
        $this->view->userRole = $this->_helper->checkUserRole->check();
        
        //var_dump($this->view->userRole);

        $model = new Model_Ad();

        $this->view->ad = $model->getOrganizations();
        $this->view->page_title .= $this->view->translate('Organizations and Campaigns');

        //paginator
        $page = $this->_getParam('page');
        if ($page) $this->view->page_title .= ' - ' . $this->view->translate('page') . ' ' . $page;
        
        $paginator = Zend_Paginator::factory($this->view->ad);
        $paginator->setDefaultScrollingStyle('Elastic');
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber($page);

        $this->view->paginator = $paginator;
    }
    
    public function orgAction() {

         $this->view->userRole = $this->_helper->checkUserRole->check();
        //var_dump($this->view->userRole);

        $id = $this->_request->getParam('id');
        $model = new Model_Ad();
        $this->view->ad = $model->getAd($id);

        //add jquery and superbox to show modal photo window
        $this->view->headScript()->appendFile(  '/js/jquery.superbox-min.js', 'text/javascript');
        $this->view->headScript()->appendFile(  '/js/superbox_settings.js', 'text/javascript');
                
        //lets count the comments number and update
        $modelComments = new Model_Comment();
        $this->view->checkCountAd = $count = $modelComments->countCommentsAd((int) $id);
        //let's increment +1 the ad view counter
        $model->updateReadedAd($id);
        $this->view->countReadedAd = $model->countReadedAd($id);


        if ($this->view->checkCountAd > 0) {
            $modelComments->updateCommentsAd($id, $count);
        }

        if ($this->view->ad != null) { // if the id ad exists then render the ad and comments
            
            $this->view->comments = $model->getComments($id);

            $this->view->page_title .= $this->view->ad['title'];
              
            //add link rel canonical , better seo
            $this->view->canonicalUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $this->lang . '/ad/org/id/' . $id . '/' . $this->view->ad['title'];

            $this->view->doctype(Zend_View_Helper_Doctype::XHTML1_RDFA);
            $this->view->headMeta()->setProperty('og:title',$this->view->page_title);
            $this->view->headMeta()->setProperty('og:type', 'product');
            $this->view->headMeta()->setProperty('og:url', $this->view->canonicalUrl);
                        
            if ($this->view->ad['photo'] !== null) $this->view->headMeta()->setProperty('og:image', 'http://www.segundamanita.com/images/uploads/ads/100/'.$this->view->ad['photo']);
            else {    
                if ($this->lang == 'es') $this->view->headMeta()->setProperty('og:image', 'http://www.segundamanita.com/images/sin_imagen.jpg');
                else $this->view->headMeta()->setProperty('og:image', 'http://www.segundamanita.com/images/no_image.jpg');
            }                        
            $this->view->headMeta()->setProperty('og:description',$this->view->ad['body']);
            $this->view->headMeta()->setProperty('fb:admins','100002353157903');

            //if user logged in, show the comment form, if not show the login link
            $auth = Zend_Auth::getInstance ();
            
            if (!$auth->hasIdentity()) {

                $this->view->createcomment = '<a class="ads_section" href="/' . $this->lang . '/auth/login">' . $this->view->translate('Login to post a comment') . '</a> ';
                $this->view->messageform = '<div class="content_section_message"><div class="private_message_link"><a class="private_message" href="/' . $this->lang . '/auth/login">' . $this->view->translate('Login to send a private message') . '</a></div></div> ';
            } else {
                require_once APPLICATION_PATH . '/forms/Comment.php';
                $form = new Form_Comment();
                $form->setAction('/' . $this->lang . '/comment/create/org_id/' . $id);

                $this->view->createcomment = $form;
                
                //Se añade enviar un mensaje privado al anunciante ad a esta página
                require_once APPLICATION_PATH . '/forms/Message.php';
                $messageform = new Form_Message();
                $messageform->setAction('/' .$this->lang . '/message/create/id_user_to/' . $this->view->ad['user_owner'].'/subject/'.$this->view->ad['title']);
        
                $this->view->messageform = $messageform;
            }
        } else {

            $this->_helper->_flashMessenger->addMessage($this->view->translate('This ad does not exist or may have been deleted!'));
            $this->_redirect('/' . $this->lang . '/ad/orgs');
        }
    }
    
    public function orgcreateAction() {

        //first we check if user is logged, if not redir to login
        $auth = Zend_Auth::getInstance ();
        if (!$auth->hasIdentity()) {

            //keep this url in zend session to redir after login
            
            $aNamespace = new Zend_Session_Namespace('Segundamanita');
            
            
            if ($this->location == null) {
                $this->_redirect('/' . $this->view->lang . '/location/change');    
            } else {
                $aNamespace->redir = $this->lang . '/ad/orgcreate';
                //Zend_Debug::dump($aNamespace->redir);
                $this->_redirect($this->lang . '/auth/login');
            }
            
        } else {

            if ($this->location == null) $this->_redirect('/' . $this->view->lang . '/location/change');    
            else {
                
                $request = $this->getRequest();
                require_once APPLICATION_PATH . '/forms/OrgCreate.php';
                $form = new Form_OrgCreate( );

                $this->view->form = $form;
                $this->view->woeidName = $this->_helper->woeid->name($this->location, $this->lang);

                if ($this->getRequest()->isPost()) {

                    if ($form->isValid($request->getPost())) {

                        $formulario = $form->getValues();

                        //create thumbnail if image exists
                        if (!empty($formulario['photo'])) {

                            $photobrut = $formulario['photo'];
                            $formulario['photo'] = $this->_createThumbnail($photobrut, '100', '90');
                        }


                        // Create a filter chain and add filters to title and body against xss, etc
                        $f = new Zend_Filter();
                        $f->addFilter(new Zend_Filter_StripTags());
                        //->addFilter(new Zend_Filter_HtmlEntities());

                        $formulario['title'] = $f->filter($formulario['title']);
                        //$formulario['body'] = $f->filter($formulario['body']);
                        $formulario['body'] = $formulario['body'];


                        //anti HOYGAN to title
                        //dont use strtolower because dont convert utf8 properly . ej: á é ó ...
                        $formulario['title'] = ucfirst(mb_convert_case($formulario['title'], MB_CASE_LOWER, "UTF-8")); 

                        //anti hoygan to body
                        $split = explode(". ", $formulario['body']);

                        foreach ($split as $sentence) {
                            $sentencegood = ucfirst(mb_convert_case($sentence, MB_CASE_LOWER, "UTF-8"));
                            $formulario['body'] = str_replace($sentence, $sentencegood, $formulario['body']);
                        }


                        //get the ip of the ad publisher
                        if (getenv(HTTP_X_FORWARDED_FOR)) $ip = getenv(HTTP_X_FORWARDED_FOR);
                        else $ip = getenv(REMOTE_ADDR);

                        $formulario['ip'] = $ip;

                        //get this ad user owner
                        $formulario ['user_owner'] = $auth->getIdentity()->id;

                        //get date created
                        //TODO to use the Zend Date object to apapt the time to the locale user zone
                        $datenow = date("Y-m-d H:i:s", time());
                        $formulario ['date_created'] = $datenow;

                        //get woeid to assign to this ad
                        //the location its stored at session location value
                        //(setted by default on bootstrap to Madrid woeid number)

                        $formulario ['woeid_code'] = $this->location;
                        //Por defecto, guardamos tipo de anuncio (Quiero)
                        $formulario['type'] = 2;  

                        $model = new Model_Ad();
                        $model->createAd($formulario);

                        $this->_helper->_flashMessenger->addMessage($this->view->translate('Ad published succesfully!'));
                        $this->_redirect('/' . $this->lang . '/ad/orgs');
                    }
                }
            }
        } 
    }
    
    public function orgeditAction() {

        //check if user logged in
        $auth = Zend_Auth::getInstance ();
        $user = new Model_User;
        $ad = new Model_Ad();

        $id = (int) $this->getRequest()->getParam('id');
        $ad_user_owner = $ad->getAd($id);

        if ($auth->hasIdentity()) {

            //$this->userRole = $this->_helper->checkUserRole->check();
            $this->view->userRole = $this->_helper->checkUserRole->check(); 
            //if user owner allow edit and show delete ad link , if not redir not allowed
            if ($this->view->userRole == 1) {//bazinga!!
            } elseif ( $user->fetchUser($auth->getIdentity()->id)->id != $ad_user_owner['user_owner'] ) {
                $this->_helper->_flashMessenger->addMessage($this->view->translate('You are not allowed to view this page'));
                $this->_redirect('/' . $this->lang . '/ad/orgs'); 
            }  

        } else {

            $this->_helper->_flashMessenger->addMessage($this->view->translate('You are not allowed to view this page'));
            $this->_redirect('/' . $this->lang . '/ad/orgs');
            return;
        }

        $this->view->deletead = '<a class="delete_ad" href="/' . $this->view->lang . '/ad/orgdelete/id/' . $this->_getParam('id') . ' ">
        <img alt="'. $this->view->translate('Delete this ad') . '" src="/images/icons/ico_delete_ad.png" class="valign" /><div class="txt_valign">'. $this->view->translate('Delete this ad') .'</div></a>';

        $request = $this->getRequest();
        require_once APPLICATION_PATH . '/forms/OrgEdit.php';
        $form = new Form_OrgEdit ();
      
        $this->view->page_title .= $this->view->translate('Edit your ad');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {

            $formData = $this->getRequest()->getPost();


            if ($form->isValid($formData)) {

                $formulario = $form->getValues();

                 //anti HOYGAN to title
                    //dont use strtolower because dont convert utf8 properly . ej: á é ó ...
                    $formulario['title'] = ucfirst(mb_convert_case($formulario['title'], MB_CASE_LOWER, "UTF-8"));

                    //anti hoygan to body
                    $split = explode(". ", $formulario['body']);

                    foreach ($split as $sentence) {
                        $sentencegood = ucfirst(mb_convert_case($sentence, MB_CASE_LOWER, "UTF-8"));
                        $formulario['body'] = str_replace($sentence, $sentencegood, $formulario['body']);
                    }

                //var_dump($form);
                //set filter againts xss and nasty things
                $f = new Zend_Filter();
                $f->addFilter(new Zend_Filter_StripTags());

                $data['title'] = $f->filter($formulario['title']);
                $data['url'] = $formulario['url'];
                $data['body'] = $formulario['body'];

                //create thumbnail if image exists
                if ($formulario['photo']) {

                    $photobrut = $formulario['photo'];
                    $data['photo'] = $this->_createThumbnail($photobrut, '100', '90');
                }

                $data['comments_enabled'] = $formulario['comments_enabled'];

                $model = new Model_Ad();
                $model->updateAd($data, $id);

                $this->_helper->_flashMessenger->addMessage($this->view->translate('Ad edited succesfully!'));
                $this->_redirect('/' . $this->lang . '/ad/org/id/' . $id);
            } else {

                $id = $this->_getParam('id');
                $ad = new Model_Ad();

                $advalues = $ad->getAd($id);
                // if photo not empty then show and let change it
                $current_photo = $advalues['photo'];
                $title = $advalues['title']; 
                if ($current_photo) {
                    $this->view->current_photo = ' <img alt="' . $title . '" src="/images/uploads/ads/100/' . $current_photo . '" />';
                }

                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id');
            if ($id > 0) {
                $ad = new Model_Ad();

                $advalues = $ad->getAd($id);
                // if photo not empty then show and let change it

                $current_photo = $advalues['photo'];
                $title = $advalues['title']; 

                if ($current_photo) {
                    $this->view->current_photo = ' <img alt="' . $title . '" src="/images/uploads/ads/100/' . $current_photo . '" />';
                }
                $form->populate($ad->getAd($id));
            }
        }
    }
    
    public function orgdeleteAction() {

        $this->view->userRole = $this->_helper->checkUserRole->check();

        $this->view->headTitle()->append($this->view->translate('Delete your profile'));

        $id = (int) $this->getRequest()->getParam('id');
        $auth = Zend_Auth::getInstance ();
        //check if user is auth
        if ($auth->hasIdentity() == FALSE) {
            $this->_helper->_flashMessenger->addMessage($this->view->translate('You are not allowed to view this page'));
            $this->_redirect('/' . $this->view->lang . '/ad/orgs');
            return;
        }

        $admodel = new Model_Ad();
        $ad = $admodel->getAd($id);


        if  (($auth->getIdentity()->id == $ad['user_owner']) || ($this->view->userRole == 1) ) {

            //if is the user owner owner lets delete it
            if ($this->getRequest()->isPost()) {
                $del = $this->getRequest()->getPost('del');

                if ($del == $this->view->translate('Yes')) {
                    //delete ad, and all his content
                    $admodel->deleteAd($id);

                    $this->_helper->_flashMessenger->addMessage($this->view->translate('Ad deleted successfully.'));
                    $this->_redirect('/' . $this->view->lang . '/ad/orgs');
                    return;
                } else {
                    $this->_helper->_flashMessenger->addMessage($this->view->translate('Nice to hear that :-)'));
                    $this->_redirect('/' . $this->view->lang . '/ad/orgs');
                    return;
                }
            } else {
                $id = $this->_getParam('id', 0);
            }
        } else {

            $this->_helper->_flashMessenger->addMessage($this->view->translate('You are not allowed to view this page'));
            $this->_redirect('/' . $this->view->lang . '/ad/orgs');
            return;
        }
    }
    
    public function listorgAction() {

        $id = (int) $this->_request->getParam('id');

        if ($id == null) {
            $this->_helper->_flashMessenger->addMessage($this->view->translate('This url does not exist'));
            //$this->_redirect('/' . $this->lang . '/woeid/' . $this->location . '/give');
            //Puede ser una organización
            $this->_redirect('/');
        }

        require_once APPLICATION_PATH . '/models/User.php';
        $modelUser = new Model_User();
        $userExists = $modelUser->fetchUser($id);
        

         if ($userExists == NULL ) {
            $this->_helper->_flashMessenger->addMessage($this->view->translate('This user does not exist'));
            //$this->_redirect('/' . $this->lang . '/woeid/' . $this->location . '/give');
            //Puede ser una organización
            $this->_redirect('/');
        }

        $model = new Model_Ad();
        $this->view->ad = $model->getAdOrglist($id);

        //paginator
        $page = $this->_getParam('page');
        
        $paginator = Zend_Paginator::factory($this->view->ad);
        $paginator->setDefaultScrollingStyle('Elastic');
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber($page);

        $this->view->paginator = $paginator;

       
        $this->user = new Model_User();

        $this->view->user = $this->user->fetchUser($id);

        $this->view->page_title .= $this->view->translate('Ad list of user') . ' ' . $this->view->user['username'];
        $page = $this->_request->getParam('page');

        if ($page) {
            $this->view->page_title .= ' - ' . $this->view->translate('page') . ' ' . $page;
        }
    }
    
    
    /********************************************************************/
    
    
    

    public function listuserAction() {

        $id = (int) $this->_request->getParam('id');

        if ($id == null) {
            $this->_helper->_flashMessenger->addMessage($this->view->translate('This url does not exist'));
            $this->_redirect('/' . $this->lang . '/woeid/' . $this->location . '/give');
        }

        require_once APPLICATION_PATH . '/models/User.php';
        $modelUser = new Model_User();
        $userExists = $modelUser->fetchUser($id);
        

         if ($userExists == NULL ) {
            $this->_helper->_flashMessenger->addMessage($this->view->translate('This user does not exist'));
            $this->_redirect('/' . $this->lang . '/woeid/' . $this->location . '/give');
        }

        $model = new Model_Ad();
        $this->view->ad = $model->getAdUserlist($id);

        //paginator
        $page = $this->_getParam('page');
        
        $paginator = Zend_Paginator::factory($this->view->ad);
        $paginator->setDefaultScrollingStyle('Elastic');
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber($page);

        $this->view->paginator = $paginator;

       
        $this->user = new Model_User();

        $this->view->user = $this->user->fetchUser($id);

        $this->view->page_title .= $this->view->translate('Ad list of user') . ' ' . $this->view->user['username'];
        $page = $this->_request->getParam('page');

        if ($page) {
            $this->view->page_title .= ' - ' . $this->view->translate('page') . ' ' . $page;
        }
    }

    public function showAction() {

         $this->view->userRole = $this->_helper->checkUserRole->check();
        //var_dump($this->view->userRole);

        $id = $this->_request->getParam('id');
        $model = new Model_Ad();
        $this->view->ad = $model->getAd($id);

        //add jquery and superbox to show modal photo window
        $this->view->headScript()->appendFile(  '/js/jquery.superbox-min.js', 'text/javascript');
        $this->view->headScript()->appendFile(  '/js/superbox_settings.js', 'text/javascript');
                
        //lets count the comments number and update
        $modelComments = new Model_Comment();
        $this->view->checkCountAd = $count = $modelComments->countCommentsAd((int) $id);
        //let's increment +1 the ad view counter
        $model->updateReadedAd($id);
        $this->view->countReadedAd = $model->countReadedAd($id);


        if ($this->view->checkCountAd > 0) {
            $modelComments->updateCommentsAd($id, $count);
        }

        if ($this->view->ad != null) { // if the id ad exists then render the ad and comments
            if ($this->view->ad['type'] == 1) {
                $this->view->page_title .= $this->view->translate('Give') . ' - ';
            }

            if ($this->view->ad['type'] == 2) {
                $this->view->page_title .= $this->view->translate('Want') . ' - ';
            }

            $this->view->comments = $model->getComments($id);
            $this->view->woeidName = $this->_helper->woeid->name($this->view->ad['woeid_code'], $this->lang);

            $this->view->page_title .= $this->view->ad['title'] . ' - ' . $this->view->woeidName;


            //add link rel canonical , better seo
            $this->view->canonicalUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $this->lang . '/ad/show/id/' . $id . '/' . $this->view->ad['title'];

                        $this->view->doctype(Zend_View_Helper_Doctype::XHTML1_RDFA);
                        $this->view->headMeta()->setProperty('og:title',$this->view->page_title);
                        $this->view->headMeta()->setProperty('og:type', 'product');
                        $this->view->headMeta()->setProperty('og:url', $this->view->canonicalUrl);
                        
                        if ($this->view->ad['photo'] !== null) $this->view->headMeta()->setProperty('og:image', 'http://www.segundamanita.com/images/uploads/ads/100/'.$this->view->ad['photo']);
                        else {    
                            if ($this->lang == 'es') $this->view->headMeta()->setProperty('og:image', 'http://www.segundamanita.com/images/sin_imagen.jpg');
                      else $this->view->headMeta()->setProperty('og:image', 'http://www.segundamanita.com/images/no_image.jpg');
                        }                        
                        $this->view->headMeta()->setProperty('og:description',$this->view->ad['body']);
                        $this->view->headMeta()->setProperty('fb:admins','100002353157903');

            //if user logged in, show the comment form, if not show the login link
            $auth = Zend_Auth::getInstance ();
            if (!$auth->hasIdentity()) {

                $this->view->createcomment = '<a class="ads_section" href="/' . $this->lang . '/auth/login">' . $this->view->translate('Login to post a comment') . '</a> ';
                $this->view->messageform = '<div class="content_section_message"><div class="private_message_link"><a class="private_message" href="/' . $this->lang . '/auth/login">' . $this->view->translate('Login to send a private message') . '</a></div></div> ';
            } else {
                require_once APPLICATION_PATH . '/forms/Comment.php';
                $form = new Form_Comment();
                $form->setAction('/' . $this->lang . '/comment/create/ad_id/' . $id);

                $this->view->createcomment = $form;
                
                //Se añade enviar un mensaje privado al anunciante ad a esta página
                    require_once APPLICATION_PATH . '/forms/Message.php';
                    $messageform = new Form_Message();
                    $messageform->setAction('/' .$this->lang . '/message/create/id_user_to/' . $this->view->ad['user_owner'].'/subject/'.$this->view->ad['title']);
        
                    $this->view->messageform = $messageform;
            }
            

        } else {

            $this->_helper->_flashMessenger->addMessage($this->view->translate('This ad does not exist or may have been deleted!'));
            $this->_redirect('/' . $this->lang . '/woeid/' . $this->location . '/give');
        }
    }

    public function createAction() {

        //first we check if user is logged, if not redir to login
        $auth = Zend_Auth::getInstance ();
        if (!$auth->hasIdentity()) {

            //keep this url in zend session to redir after login
            
            $aNamespace = new Zend_Session_Namespace('Segundamanita');
            
            if ($this->location == null) {
                       $this->_redirect('/' . $this->view->lang . '/location/change');    
                  } else {
                $aNamespace->redir = $this->lang . '/ad/create';
                            //Zend_Debug::dump($aNamespace->redir);
                $this->_redirect($this->lang . '/auth/login');
                        }
            
        } else {

                    if ($this->location == null) {
                   $this->_redirect('/' . $this->view->lang . '/location/change');    
             
              } else {
                  
            $request = $this->getRequest();
            require_once APPLICATION_PATH . '/forms/AdCreate.php';
            $form = new Form_AdCreate( );

            $this->view->form = $form;
            $this->view->woeidName = $this->_helper->woeid->name($this->location, $this->lang);


            if ($this->getRequest()->isPost()) {

                if ($form->isValid($request->getPost())) {

                    $formulario = $form->getValues();

                    //create thumbnail if image exists
                    if (!empty($formulario['photo'])) {

                        $photobrut = $formulario['photo'];
                        $formulario['photo'] = $this->_createThumbnail($photobrut, '100', '90');
                    }


                    // Create a filter chain and add filters to title and body against xss, etc
                    $f = new Zend_Filter();
                    $f->addFilter(new Zend_Filter_StripTags());
                    //->addFilter(new Zend_Filter_HtmlEntities());

                    $formulario['title'] = $f->filter($formulario['title']);
                    $formulario['body'] = $f->filter($formulario['body']);

                    //anti HOYGAN to title
                    //dont use strtolower because dont convert utf8 properly . ej: á é ó ...
                    $formulario['title'] = ucfirst(mb_convert_case($formulario['title'], MB_CASE_LOWER, "UTF-8"));

                    //anti hoygan to body
                    $split = explode(". ", $formulario['body']);

                    foreach ($split as $sentence) {
                        $sentencegood = ucfirst(mb_convert_case($sentence, MB_CASE_LOWER, "UTF-8"));
                        $formulario['body'] = str_replace($sentence, $sentencegood, $formulario['body']);
                    }


                    //get the ip of the ad publisher
                    if (getenv(HTTP_X_FORWARDED_FOR)) {
                        $ip = getenv(HTTP_X_FORWARDED_FOR);
                    } else {
                        $ip = getenv(REMOTE_ADDR);
                    }

                    $formulario['ip'] = $ip;

                    //get this ad user owner
                    $formulario ['user_owner'] = $auth->getIdentity()->id;

                    //get date created
                    //TODO to use the Zend Date object to apapt the time to the locale user zone
                    $datenow = date("Y-m-d H:i:s", time());
                    $formulario ['date_created'] = $datenow;

                    //get woeid to assign to this ad
                    //the location its stored at session location value
                    //(setted by default on bootstrap to Madrid woeid number)

                    $formulario ['woeid_code'] = $this->location;

                    $model = new Model_Ad();
                    $model->createAd($formulario);

                    $this->_helper->_flashMessenger->addMessage($this->view->translate('Ad published succesfully!'));
                    $this->_redirect('/' . $this->lang . '/woeid/' . $this->location . '/give');
                }
            }
        }
      }  
    }

    public function editAction() {

        //check if user logged in
        $auth = Zend_Auth::getInstance ();
        $user = new Model_User;
        $ad = new Model_Ad();

        $id = (int) $this->getRequest()->getParam('id');
        $ad_user_owner = $ad->getAd($id);

        if ($auth->hasIdentity()) {

            $this->userRole = $this->_helper->checkUserRole->check();
            //if user owner allow edit and show delete ad link , if not redir not allowed

            if ($this->userRole == 1) {
                //bazinga!!
            }
            elseif ( $user->fetchUser($auth->getIdentity()->id)->id != $ad_user_owner['user_owner'] ) {
                $this->_helper->_flashMessenger->addMessage($this->view->translate('You are not allowed to view this page'));
                $this->_redirect('/' . $this->lang . '/woeid/' . $this->location . '/give');
            }  

        } else {

            $this->_helper->_flashMessenger->addMessage($this->view->translate('You are not allowed to view this page'));
            $this->_redirect('/' . $this->lang . '/woeid/' . $this->location . '/give');
            return;
        }

        $this->view->deletead = '<a class="delete_ad" href="/' . $this->view->lang . '/ad/delete/id/' . $this->_getParam('id') . ' ">
        <img alt="'. $this->view->translate('Delete this ad') . '" src="/images/icons/ico_delete_ad.png" class="valign" /><div class="txt_valign">'. $this->view->translate('Delete this ad') .'</div></a>';

        $request = $this->getRequest();
        require_once APPLICATION_PATH . '/forms/AdEdit.php';
        $form = new Form_AdEdit ( );


        /*$form->addElement('select', 'status', array(
            'order' => '1',
            'label' => 'Status:', 'required' => true,
            'multioptions' => array('available' => 'available', 'booked' => 'booked', 'delivered' => 'delivered')));*/
       
        $this->view->page_title .= $this->view->translate('Edit your ad');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {

            $formData = $this->getRequest()->getPost();
            
            if ($form->isValid($formData)) {

                $formulario = $form->getValues();

                 //anti HOYGAN to title
                 //dont use strtolower because dont convert utf8 properly . ej: á é ó ...
                 $formulario['title'] = ucfirst(mb_convert_case($formulario['title'], MB_CASE_LOWER, "UTF-8"));

                 //anti hoygan to body
                 $split = explode(". ", $formulario['body']);

                 foreach ($split as $sentence) {
                    $sentencegood = ucfirst(mb_convert_case($sentence, MB_CASE_LOWER, "UTF-8"));
                    $formulario['body'] = str_replace($sentence, $sentencegood, $formulario['body']);
                 }

                //var_dump($form);
                //set filter againts xss and nasty things
                $f = new Zend_Filter();
                $f->addFilter(new Zend_Filter_StripTags());

                $data['title'] = $f->filter($formulario['title']);
                $data['body'] = $f->filter($formulario['body']);
                $data['type'] = $f->filter($formulario['type']);

                //create thumbnail if image exists
                if ($formulario['photo']) {

                    $photobrut = $formulario['photo'];
                    $data['photo'] = $this->_createThumbnail($photobrut, '100', '90');
                }


                $data['status'] = $formulario['status'];
                $data['comments_enabled'] = $formulario['comments_enabled'];

                $model = new Model_Ad();
                $model->updateAd($data, $id);

                $this->_helper->_flashMessenger->addMessage($this->view->translate('Ad edited succesfully!'));
                $this->_redirect('/' . $this->lang . '/ad/show/id/' . $id);
            } else {

                $id = $this->_getParam('id');
                $ad = new Model_Ad();

                $advalues = $ad->getAd($id);
                // if photo not empty then show and let change it
                $current_photo = $advalues['photo'];
                $title = $advalues['title']; 
                if ($current_photo) {
                    $this->view->current_photo = ' <img alt="' . $title . '" src="/images/uploads/ads/100/' . $current_photo . '" />';
                }

                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id');
            if ($id > 0) {
                $ad = new Model_Ad();

                $advalues = $ad->getAd($id);
                // if photo not empty then show and let change it

                $current_photo = $advalues['photo'];
                $title = $advalues['title']; 

                if ($current_photo) {
                    $this->view->current_photo = ' <img alt="' . $title . '" src="/images/uploads/ads/100/' . $current_photo . '" />';
                }
                $form->populate($ad->getAd($id));
            }
        }
    }


    public function deleteAction() {

        $this->userRole = $this->_helper->checkUserRole->check();

        $this->view->headTitle()->append($this->view->translate('Delete your profile'));

        $id = (int) $this->getRequest()->getParam('id');
        $auth = Zend_Auth::getInstance ();
        //check if user is auth
        if ($auth->hasIdentity() == FALSE) {
            $this->_helper->_flashMessenger->addMessage($this->view->translate('You are not allowed to view this page'));
            $this->_redirect('/' . $this->view->lang . '/ad/list/woeid/' . $this->location . '/ad_type/give');
            return;
        }

        $admodel = new Model_Ad();
        $ad = $admodel->getAd($id);


        if  (($auth->getIdentity()->id == $ad['user_owner']) || ($this->userRole == 1) ) {

            //if is the user owner owner lets delete it
            if ($this->getRequest()->isPost()) {
                $del = $this->getRequest()->getPost('del');

                if ($del == $this->view->translate('Yes')) {
                    //delete ad, and all his content
                    $admodel->deleteAd($id);

                    $this->_helper->_flashMessenger->addMessage($this->view->translate('Ad deleted successfully.'));
                    $this->_redirect('/' . $this->view->lang . '/ad/list/woeid/' . $this->location . '/ad_type/give');
                    return;
                } else {
                    $this->_helper->_flashMessenger->addMessage($this->view->translate('Nice to hear that :-)'));
                    $this->_redirect('/' . $this->view->lang . '/ad/list/woeid/' . $this->location . '/ad_type/give');
                    return;
                }
            } else {
                $id = $this->_getParam('id', 0);
            }
        } else {

            $this->_helper->_flashMessenger->addMessage($this->view->translate('You are not allowed to view this page'));
            $this->_redirect('/' . $this->view->lang . '/ad/list/woeid/' . $this->location . '/ad_type/give');
            return;
        }
    }



    /*
     * _createThumbnail uses resize class
     *
     */

    protected function _createThumbnail($file, $x, $y) {

        require_once ( SMANITA_PATH . '/library/SimpleImage.php' );

        $file_ext = substr(strrchr($file, '.'), 1);
        $fileuniquename = md5(uniqid(mktime())) . '.' . $file_ext;

        $image = new SimpleImage();
        $image->load('/tmp/' . $file);

        //save original to right place
        $widthmax = 900;
        $image->resizeToWidthMax($widthmax);
        $image->save(SMANITA_PATH . '/www/images/uploads/ads/original/' . $fileuniquename);

        //save thumb 100
        $image->resizeToWidth($x);
        $image->save(SMANITA_PATH . '/www/images/uploads/ads/100/' . $fileuniquename);

        return $fileuniquename;
    }
    
}